import { v4 as uuid } from 'uuid'
import { StdTextDataV2, StdTextsV2 } from './textData'

export class StdBulletData {
  constructor({ dataId, bulletList = [], dataLang = 'zh' }) {
    this.dataId = dataId || uuid()
    this.dataLang = dataLang
    this.bulletList = bulletList.map((i) => new StdBullet(i))
  }
}

class StdBullet {
  constructor({ dataId, shareId, type = 'lower', level = '1', levelNum = '1', dataLang = 'zh', textData }) {
    // 数据唯一标识
    this.dataId = dataId || uuid()
    // 中英文共享id-联动场景使用
    // this.shareId = shareId
    // 项目符号类型：值包括：lower（小写字母），num（数字:1)），roman(罗马数字)，num2(数字:[1])
    this.type = type
    // 层级编号 示例：1,2,3,4
    this.level = level
    // 层级 示例：1，1.1，1.1.1，2...
    this.levelNum = levelNum
    // 中英文区分 zh:中文，en:英文
    this.dataLang = dataLang
    // 如果文本，以换行分割数组
    this.textData = new StdTextDataV2(textData)
  }
}

export class StdBulletListV2 {
  constructor(nodes) {
    this.nodes = nodes
  }
  get data() {
    return this.parseBulletList(this.nodes)
  }
  parseBulletList(blocks) {
    function walk(node, _array) {
      const attrs = node?.dataset || {}
      let res = {
        dataId: attrs.noteId || uuid(),
        // shareId: attrs.shareId,
        levelNum: attrs.levelNum,
        level: attrs.level,
        type: attrs.action,
        dataLang: attrs.lang,
        textData: new StdBulletTextDataV2(node.firstChild).data
      }
      _array.push(res)
    }
    const block = blocks[0]
    const attrMap = block?.dataset || {}
    let result = {
      dataId: attrMap.blockDataId,
      dataLang: attrMap.lang,
      bulletList: []
    }
    for (let i = 0, len = blocks.length; i < len; i++) {
      walk(blocks[i], result.bulletList)
    }
    return result
  }
}
class StdBulletTextDataV2 extends StdTextsV2 {
  constructor(node) {
    super(node)
  }
  get data() {
    this.getNumberContent(this.node)
    this.getContent(this.node)
    return {
      dataId: this.node?.parentNode?.dataset?.id,
      isExample: Number(this.node?.parentNode?.dataset?.isExample) ? true : false,
      contentList: this.contentList
    }
  }
  // 拼接列项前面的编号
  getNumberContent(node) {
    this.contentList.push({
      contentType: 'text',
      contentNumber: node?.parentNode?.dataset?.number || null,
      content: '',
      fullContent: this.getFullNumber(node)
    })
  }
  getFullNumber(node) {
    const attrMap = node?.parentNode?.dataset || {}
    const grade = parseInt(attrMap.grade)
    const number = attrMap.number
    if (grade !== 4) {
      return `${number})`
    }
    return `[${number}]`
  }
}
